﻿using System;
using Microsoft.SharePoint;
using Repo.DataAccess;
using Repo.Enums;

namespace Repo.DataCaching
{
    public class CacheReseter
    {
        public static void ResetCache(SPWeb rootWeb, string contentTypeName, string listTitle)
        {
            //Anonymous and Authenticated Reset
            foreach (var userType in Enum.GetValues(typeof(UserType)))
            {
                var key = KeyGenerator.GenerateKey(userType.ToString(), contentTypeName, listTitle);
                var dt = rootWeb.AllProperties[key];
                if (dt != null)
                {
                    rootWeb.Site.RootWeb.AllProperties.Remove(key);
                }
                rootWeb.Site.RootWeb.AllProperties.Add(key, DateTime.Now);
                rootWeb.Site.RootWeb.Update();
            }
        }

        public static void ResetCache(SPItemEventProperties properties)
        {
            var rootWeb = properties.Web.Site.RootWeb;
            var contentTypeName = properties.ListItem.ContentType.Name;
            var listTitle = properties.List.Title;
            ResetCache(rootWeb, contentTypeName, listTitle);
        }
    }
}
